﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace VirtualDisk
{
    class DiskHelper
    {
       
        //判断路径是否是绝对路径
        public static bool isAbsolutePath(MyString str)
        {
            if (MyString.IsNullOrEmpty(str))
            {
                return false;
            }
            MyString rootName = DiskTree.getInstance().getRootPath();
            MyString[] name = str.Split('\\');
            if (name[0].Trim().ToLower() == rootName.ToLower()||MyString.IsNullOrEmpty(name[0]))
                return true;
            else
                return false;
        }

        //判断目录或文件名是否合法
        public static bool isLegalName(MyString name)   
        {

            if (MyString.IsNullOrEmpty(name))
            {
                return false;
            }
            if (name.Length != name.TrimStart().Length)
            {
                return false;
            }
            if (name.Trim().Length > 256)
            {
                return false;
            }
            char[] chars={'<','>',':','|','?','"','*','\\','/'};
            foreach (char c in chars)
            {
                if (name.Contains(c))
                {
                    return false;
                }
            }
            if (name.Length!=2)
            {
                for(int i=0;i<name.Length;i++)
                {
                    if (name[i] != '.')
                    {
                        return true;
                    }
                }
                return false;
            }
            return true;
        }

        //获得路径中的文件名
        public static MyString getFileName(MyString path)
        {
            return new MyString(Path.GetFileName(path.ToString())); 
        }
        //获得目录
        public static MyString getDirectoryName(MyString path)
        {
           return  new MyString(Path.GetDirectoryName(path.ToString()));
        }
        //获得扩展名
        public static MyString getExtension(MyString path)
        {
            return new MyString(Path.GetExtension(path.ToString()));
        }

        //判断路径是否合法
        public static bool isLegalPath(MyString path)
        {
            if (MyString.IsNullOrEmpty(path))
            {
                return false;
            }
            MyString[] NameInPath = path.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries);
            int i = isAbsolutePath(path)?1:0;
            for (; i < NameInPath.Length;i++ )
            {
                if (!isLegalName(NameInPath[i]))
                {
                    return false;
                }
            }
            return true;
        }
        //对含有通配符的字符串进行处理，使其可以正则表达式匹配
        public static MyString getCorrectWildcradName(MyString str)
        {
            StringBuilder sb = new StringBuilder(str.ToString());
            
            //对字符串中特殊字符处理，防止匹配成正则表达式中的专有字符
            sb.Replace("\\", "\\\\");
            sb.Replace("^", "\\^");
            sb.Replace("$", "\\$");
            sb.Replace("+", "\\+");
            sb.Replace("{", "\\{");
            sb.Replace("}", "\\}");
            sb.Replace(".", "\\.");
            sb.Replace("[", "\\[");
            sb.Replace("]", "\\]");
            sb.Replace("(", "\\(");
            sb.Replace(")", "\\)");
            
            //将*换成.*   将？换成.?
            sb.Replace("*", ".*");
            sb.Replace("?", ".?");
            sb.Append("$");
            return new MyString("^" + sb.ToString());
        }
       

    }
}
